﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fmis21_10.Models;

namespace Fmis21_10.Controllers
{
    public class GoalHistoryController : Controller
    {
        private FmisEntities db = new FmisEntities();

        //
        // GET: /GoalHistory/

        public ActionResult Index()
        {
            var goalhistories = db.GoalHistories.Include(g => g.Player).Include(g => g.MatchInformation);
            return View(goalhistories.ToList());
        }

        //
        // GET: /GoalHistory/Details/5

        public ActionResult Details(int id = 0)
        {
            GoalHistory goalhistory = db.GoalHistories.Find(id);
            if (goalhistory == null)
            {
                return HttpNotFound();
            }
            return View(goalhistory);
        }
        public PartialViewResult MultiCreate()
        {
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "PlayerName");
            ViewBag.MatchInformationId = new SelectList(db.MatchInformations, "MatchInformationId", "MatchInformationId");
            return PartialView("MultiCreate");
        }
        //
        // GET: /GoalHistory/Create

        public ActionResult Create()
        {
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName");
            ViewBag.MatchInformationId = new SelectList(db.MatchInformations, "MatchInformationId", "MatchInformationId");
            return View();
        }

        //
        // POST: /GoalHistory/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(GoalHistory goalhistory, FormCollection f)
        {
            if (ModelState.IsValid)
            {
                var iformationmatchIdArray = f.GetValues("MatchInformationId");
                var playerIdArray = f.GetValues("PlayerId");
                var timegoalArray = f.GetValues("TimeGoal");

                for (int i = 0; i < playerIdArray.Count(); i++)
                {
                    goalhistory.MatchInformationId = Convert.ToInt32(iformationmatchIdArray[i]);
                    goalhistory.PlayerId = Convert.ToInt32(playerIdArray[i]);
                    goalhistory.TimeGoal = timegoalArray[i];

                    db.GoalHistories.Add(goalhistory);
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName", goalhistory.PlayerId);
            ViewBag.MatchInformationId = new SelectList(db.MatchInformations, "MatchInformationId", "MatchInformationId", goalhistory.MatchInformationId);
            return View(goalhistory);
        }

        //
        // GET: /GoalHistory/Edit/5

        public ActionResult Edit(int id = 0)
        {
            GoalHistory goalhistory = db.GoalHistories.Find(id);
            if (goalhistory == null)
            {
                return HttpNotFound();
            }
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName", goalhistory.PlayerId);
            ViewBag.MatchInformationId = new SelectList(db.MatchInformations, "MatchInformationId", "MatchInformationId", goalhistory.MatchInformationId);
            return View(goalhistory);
        }

        //
        // POST: /GoalHistory/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(GoalHistory goalhistory)
        {
            if (ModelState.IsValid)
            {
                db.Entry(goalhistory).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName", goalhistory.PlayerId);
            ViewBag.MatchInformationId = new SelectList(db.MatchInformations, "MatchInformationId", "MatchInformationId", goalhistory.MatchInformationId);
            return View(goalhistory);
        }

        //
        // GET: /GoalHistory/Delete/5

        public ActionResult Delete(int id = 0)
        {
            GoalHistory goalhistory = db.GoalHistories.Find(id);
            if (goalhistory == null)
            {
                return HttpNotFound();
            }
            return View(goalhistory);
        }

        //
        // POST: /GoalHistory/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            GoalHistory goalhistory = db.GoalHistories.Find(id);
            db.GoalHistories.Remove(goalhistory);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}